package com.pjh.shitang.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pjh.shitang.entitys.Comment;
import com.pjh.shitang.entitys.Likes;
import com.pjh.shitang.entitys.Reply;
import com.pjh.shitang.mappers.CommentMapper;
import com.pjh.shitang.mappers.LikesMapper;
import com.pjh.shitang.mappers.ReplyMapper;
import com.pjh.shitang.mappers.WindowMapper;
import com.pjh.shitang.services.CommentService;
import com.pjh.shitang.utils.helpers.PropertiesHelper;
import com.pjh.shitang.utils.vo.receive.LikeInfo;
import com.pjh.shitang.utils.vo.receive.PageInfo;
import com.pjh.shitang.utils.vo.receive.PublishInfo;
import com.pjh.shitang.utils.vo.returns.DataWithLike;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author yueyinghaibao
 * @date 2022/5/1
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    CommentMapper commentMapper;

    @Resource
    ReplyMapper replyMapper;

    @Resource
    LikesMapper likesMapper;

    @Resource
    WindowMapper windowMapper;

    @Override
    public boolean save(PublishInfo info) {
        if(PropertiesHelper.isAnyNull(info)) {
            throw new RuntimeException("参数为空");
        }

        int res;

        if("comment".equals(info.getType())) {
            res = commentMapper.insert(new Comment(info.getFroms(), info.getTos(), new Date(), info.getContent()));
        } else {
            res = replyMapper.insert(new Reply(info.getFroms(), info.getTos(), new Date(), info.getContent()));
        }

        return res > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addLike(LikeInfo info) {
        if(PropertiesHelper.isAnyNull(info)) {
            throw new RuntimeException("参数为空");
        }

        int res, res2 = 0;

        if("reply".equals(info.getType())) {
            res = replyMapper.update(1, info.getTos());
        } else if("window".equals(info.getType())) {
            res = windowMapper.update(1, info.getTos());
        } else {
            res = commentMapper.update(1, info.getTos());
        }

        if(res == 0) {
            throw new RuntimeException("您回复的对象不存在");
        }

        Map<String, Object> map = new TreeMap<>();
        map.put("froms", info.getFroms());
        map.put("tos", info.getTos());
        map.put("type", info.getType());

        if(likesMapper.selectCount(new QueryWrapper<Likes>().allEq(map)) == 0) {
            res2 = likesMapper.insert(new Likes(info.getFroms(), info.getTos(), info.getType()));
        }

        return res > 0 && res2 > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean decLike(LikeInfo info) {
        if(PropertiesHelper.isAnyNull(info)) {
            throw new RuntimeException("参数为空");
        }

        int res;

        if("reply".equals(info.getType())) {
            res = replyMapper.update(-1, info.getTos());
        } else if("window".equals(info.getType())) {
            res = windowMapper.update(-1, info.getTos());
        } else {
            res = commentMapper.update(-1, info.getTos());
        }

        Map<String, Object> map = new TreeMap<>();
        map.put("froms", info.getFroms());
        map.put("tos", info.getTos());
        map.put("type", info.getType());

        int res2 = likesMapper.delete(new QueryWrapper<Likes>().allEq(map));

        return res > 0 && res2 > 0;
    }

    @Override
    public boolean countLike(LikeInfo info) {
        if(PropertiesHelper.isAnyNull(info)) {
            throw new RuntimeException("参数为空");
        }

        Map<String, Object> map = new TreeMap<>();
        map.put("froms", info.getFroms());
        map.put("tos", info.getTos());
        map.put("type", info.getType());

        long res = likesMapper.selectCount(new QueryWrapper<Likes>().allEq(map));

        return res > 0;
    }

    @Override
    public List<DataWithLike<Object>> queryPage(PageInfo info) {
        if(PropertiesHelper.isAnyNull(info)) {
            throw new RuntimeException("参数为空");
        }
        List<DataWithLike<Object>> list = new ArrayList<>();

        if("reply".equals(info.getType())) {
            Page<Reply> page1 = new Page<>(info.getPage(), info.getSize());
            page1 = replyMapper.selectPage(page1, new LambdaQueryWrapper<Reply>().eq(Reply::getTos, info.getTos()));
            for(Reply c : page1.getRecords()) {
                list.add(new DataWithLike<>(c,
                        likesMapper.selectCount(new LambdaQueryWrapper<Likes>()
                                .eq(Likes::getFroms, info.getFroms())
                                .eq(Likes::getTos, c.getTos())
                                .eq(Likes::getType, info.getType())) > 0));
            }
        } else {
            Page<Comment> page1 = new Page<>(info.getPage(), info.getSize());
            page1 = commentMapper.selectPage(page1, new LambdaQueryWrapper<Comment>().eq(Comment::getTos, info.getTos()));
            for(Comment c : page1.getRecords()) {
                list.add(new DataWithLike<>(c,
                        likesMapper.selectCount(new LambdaQueryWrapper<Likes>()
                                .eq(Likes::getFroms, info.getFroms())
                                .eq(Likes::getTos, c.getTos())
                                .eq(Likes::getType, info.getType())) > 0));
            }
        }

        return list;
    }
}
